package com.lenovo.tool;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

class XmlResFileFilter {
    private ArrayList<String> fileNames = new ArrayList<String>();
    private ArrayList<String> excludePaths = new ArrayList<String>();

    public XmlResFileFilter(boolean inclArray) throws IOException {
    	String[] strings = FileFilter.getInstance().getStringFiles();
    	for (String fileName : strings)
		{
    		fileNames.add(fileName);
		}
    	strings = null;
        if (inclArray) {
        	String[] arrays = FileFilter.getInstance().getArrayFiles();
        	for (String fileName : arrays)
    		{
        		fileNames.add(fileName);
    		}
        	arrays = null;
        }
    }

    public void addExludePath(String path) {
        this.excludePaths.add(path);
    }

    public boolean shallExclude(String relativePath) {
        for (String fitlerPath : excludePaths) {
            if (fitlerPath.startsWith(File.separator)) {
                if (relativePath.contains(fitlerPath)) {
                    return true;
                }
            } else {
                if (relativePath.equals(fitlerPath)) {
                    return true;
                }
            }
        }
        return false;
    }

    private ArrayList<File> listXmlResFiles(String relativePath, File dir,
            ArrayList<String> tailPathList) {
        ArrayList<File> fileList = new ArrayList<File>();
        File[] files = dir.listFiles();

        for (int k = 0; k < files.length; k++) {
            String path = relativePath;
            if (path == null || path.length() == 0) {
                path = files[k].getName();
            } else {
                path = relativePath + File.separator + files[k].getName();
            }

            if (shallExclude(path)) {
                continue;
            }

            if (files[k].isFile()) {
                for (String tailPath : tailPathList) {
                    if (files[k].getPath().endsWith(tailPath)) {
                        fileList.add(files[k]);
                        break;
                    }
                }
            } else if (files[k].isDirectory()) {
                fileList.addAll(listXmlResFiles(path, files[k], tailPathList));
            }
        }
        return fileList;
    }

    public String getXmlResTailPath(String locale, String fileName) {
        StringBuffer buf = new StringBuffer();

        buf.append("res");
        buf.append(File.separator);
        buf.append("values");

        if (locale != null && locale.trim().length() > 0  && !locale.equals("en")) {
            buf.append("-");
            buf.append(locale);
        }
        buf.append(File.separator);
        buf.append(fileName);

        return buf.toString();
    }

    public ArrayList<String> getXmlResTailPath(String locale) {
        ArrayList<String> list = new ArrayList<String>();

        StringBuffer buf = new StringBuffer();

        buf.append("res");
        buf.append(File.separator);
        buf.append("values");

        if (locale != null && locale.trim().length() > 0 && !locale.equals("en")) {
            buf.append("-");
            buf.append(locale);
        }
        buf.append(File.separator);

        for (String name : fileNames) {
            list.add(buf.toString() + name);
        }

        return list;
    }

    public ArrayList<File> getAllXmlResFiles(File dir) {
        ArrayList<String> tailPathList = new ArrayList<String>();
        for (String name : fileNames) {
            tailPathList.add(File.separator + name);
        }

        return listXmlResFiles("", dir, tailPathList);
    }

    public ArrayList<File> getLocaleXmlResFiles(File dir, String locale) {
        ArrayList<String> tailPathList = new ArrayList<String>();
        tailPathList.addAll(getXmlResTailPath(locale));
        return listXmlResFiles("", dir, tailPathList);
    }

    public ArrayList<File> getLocaleXmlResFiles(File dir,
            ArrayList<String> locales) {
        ArrayList<String> tailPathList = new ArrayList<String>();
        for (String locale : locales) {
            tailPathList.addAll(getXmlResTailPath(locale));
        }

        return listXmlResFiles("", dir, tailPathList);
    }

}
